package cn.s1995.sloth.bms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.VideoComment;
import cn.s1995.sloth.bizs.service.VideoCommentService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 视频评论
 *
 * @author Sasshi
 * @date 2023-12-13 10:30:37
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/videoComment")
@Tag(description = "VideoComment", name = "BMS-视频评论管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class VideoCommentController {

    private final VideoCommentService videoCommentService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param req 视频评论
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_VideoComment_view')")
    public R getVideoCommentPage(@ParameterObject Page page, @ParameterObject VideoComment req) {
        LambdaQueryWrapper<VideoComment> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(req.getVideoId()), VideoComment::getVideoId, req.getVideoId());
		wrapper.eq(Objects.nonNull(req.getPid()), VideoComment::getPid, req.getPid());
		wrapper.eq(Objects.nonNull(req.getState()), VideoComment::getState, req.getState());
		wrapper.eq(Objects.nonNull(req.getUid()), VideoComment::getUid, req.getUid());
		wrapper.like(StrUtil.isNotBlank(req.getContent()), VideoComment::getContent, req.getContent());
		wrapper.eq(Objects.nonNull(req.getIsAnonymous()), VideoComment::getIsAnonymous, req.getIsAnonymous());
		wrapper.orderByDesc(VideoComment::getCreateTime);
        return R.ok(videoCommentService.page(page, wrapper));
    }


    /**
     * 通过id查询视频评论
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_VideoComment_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(videoCommentService.getById(id));
    }

    /**
     * 新增视频评论
     * @param req 视频评论
     * @return R
     */
    @Operation(summary = "新增视频评论", description = "新增视频评论")
    @SysLog("新增视频评论")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_VideoComment_add')")
    public R save(@RequestBody VideoComment req) {
        return R.ok(videoCommentService.save(req));
    }

    /**
     * 修改视频评论
     * @param req 视频评论
     * @return R
     */
    @Operation(summary = "修改视频评论", description = "修改视频评论")
    @SysLog("修改视频评论")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_VideoComment_edit')")
    public R updateById(@RequestBody VideoComment req) {
        return R.ok(videoCommentService.updateById(req));
    }

    /**
     * 通过id删除视频评论
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除视频评论", description = "通过id删除视频评论")
    @SysLog("通过id删除视频评论")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_VideoComment_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(videoCommentService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_VideoComment_export')")
    public List<VideoComment> export(VideoComment req, Long[] ids) {
        return videoCommentService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), VideoComment::getId, ids));
    }
}
